/*
 * Autenticacion.java
 *
 * Created on 27 de octubre de 2008, 8:51
 */

package jcpcotizaciones.vista;


import javax.swing.JOptionPane;
import jcpcotizaciones.Control.ControlUsuario;
import jcpcotizaciones.JCPCOTIZACIONESApp;
import jcpcotizaciones.JCPCOTIZACIONESView;
import jcpcotizaciones.Modelo.*;
/**
 *
 * @author  Administrador
 */
public class inicioSesion extends javax.swing.JFrame {
    
    /** Creates new form Autenticacion */
    public inicioSesion() {
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tx_clave = new javax.swing.JPasswordField();
        tx_nombre = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        bt_iniciar = new javax.swing.JButton();
        bt_cancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Iniciar Sesion en ::LITOSOFT::"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), " :: Inicio de Sesion ::  ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N

        tx_clave.setFont(new java.awt.Font("Tahoma", 1, 14));
        tx_clave.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        tx_clave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tx_claveActionPerformed(evt);
            }
        });

        tx_nombre.setFont(new java.awt.Font("Tahoma", 1, 14));
        tx_nombre.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        tx_nombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tx_nombreActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Usuario:");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setText("Password:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tx_nombre, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                    .addComponent(tx_clave, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(tx_nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tx_clave, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        bt_iniciar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jcpcotizaciones/resources/iconos/48x48/Fermer.png"))); // NOI18N
        bt_iniciar.setToolTipText("Ingresar y Comenzar");
        bt_iniciar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bt_iniciar.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        bt_iniciar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bt_iniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_iniciarActionPerformed(evt);
            }
        });

        bt_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jcpcotizaciones/resources/iconos/48x48/Areter.png"))); // NOI18N
        bt_cancelar.setToolTipText("Terminar y Salir");
        bt_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(116, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(bt_iniciar, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(bt_cancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bt_iniciar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bt_cancelar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tx_nombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tx_nombreActionPerformed
// TODO: Agrege su codigo aqui:
       tx_clave.requestFocus();
        
    }//GEN-LAST:event_tx_nombreActionPerformed

    private void tx_claveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tx_claveActionPerformed
        inicio();
    }//GEN-LAST:event_tx_claveActionPerformed

    private void bt_iniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_iniciarActionPerformed
    inicio();
    }//GEN-LAST:event_bt_iniciarActionPerformed

    private void bt_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelarActionPerformed
       System.exit(0);
        // TODO add your handling code here:
    }//GEN-LAST:event_bt_cancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new inicioSesion().setVisible(true);
            }
        });
    }

public void inicio() /*Aquí capturamos los dos campos, el del nombre del usuario y el de la contraseña
                       para luego validarlos en una busqueda para poder iniciar seción.
                       El unsario debe estar previamente registrado.*/
{
    Usuario usu = ControlUsuario.iniciarSesion(tx_nombre.getText(), tx_clave.getText());
    if(usu!= null){
        if (usu.getCargo().equals("VENDEDOR")){
           JCPCOTIZACIONESApp.usu=usu;
            this.setVisible(false);
            JCPCOTIZACIONESApp p = JCPCOTIZACIONESApp.getApplication();
            p.startup();
            JCPCOTIZACIONESView.menu_usuario.setVisible(false);
            JCPCOTIZACIONESView.bt_usuario.setVisible(false);
            JCPCOTIZACIONESView.tx_usuario.setText(usu.getNombre());
        }else{
           if (usu.getCargo().equals("ADMINISTRADOR")){
        JCPCOTIZACIONESApp.usu=usu;
        this.setVisible(false);
        JCPCOTIZACIONESApp p = JCPCOTIZACIONESApp.getApplication();
        p.startup();
        JCPCOTIZACIONESView.tx_usuario.setText(usu.getNombre());
           } else{
               JOptionPane.showMessageDialog(null,"El perfil de usuario no existe");

           }
    }

            } else{
                JOptionPane.showMessageDialog(null,"no se pudo iniciar sesion");
                tx_clave.setText("");
            }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_cancelar;
    private javax.swing.JButton bt_iniciar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField tx_clave;
    private javax.swing.JTextField tx_nombre;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the inicio
     */
 
    
}
